#!/bin/bash
#######
## mysql5.6,mysql5.7用
## yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
## percona-release enable-only tools release(可选项,按照实际情况添加)
## yum install -y percona-xtrabackup-24
## 卸载yum remove percona-xtrabackup
#######
##  使用
##  1创建文件夹及其路径 mkdir -p /home/backup/full  &&  mkdir -p /home/backup/incremental  && mkdir -p /home/backup/gzip  && mkdir -p /home/backup/tar.gzdb  && mkdir -p /home/backup/config
##  2在配置文件中添加/home/backup/config/config
##       backup_full=
##       backup_pre_name=
##  3 bash 进行执行脚本
#日期转为天数
function date2days() {
    echo "$*" | awk '{
        z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
        j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
        print j
    }'
}

#######################
db_user="root" #数据库的用户名
db_passwd=""  #数据库的密码
db_databases="test"  #备份的数据库多个用空格隔开
db_defaults_file="/etc/my.cnf" #备份后的配置文件
db_socket="/tmp/mysql.sock"  #mysql的tcp通信的sock文件
db_backup="/home/backup/"  #备份的目录
db_backup_fulldir="/home/backup/full/"  #全量备份
db_backup_incrementaldir="/home/backup/incremental/" #增量备份的文件夹
db_backup_gzfull="/home/backup/gzip/"  #gzip压缩后的文件夹
db_backup_tarfull="/home/backup/tar.gzdb/" #tar压缩文件
rm_num=7  #多少天一个循环

#用于压缩并转移源文件
move_and_tar() {
    if [ $# != 1 ]; then
        echo "参数不正确"
        exit 0
    fi

    time_rm=$(date -d "$1 days ago" +"back_%d-%m-%Y")

    if [ $1 -eq 7 ]; then
        if [ -d ${db_backup_fulldir}${time_rm} ]; then
            su - root -c "tar -czPvf ${db_backup_tarfull}${time_rm}_full.tar.gz ${db_backup_fulldir}${time_rm}"
            su - root -c "rm -rf ${db_backup_fulldir}${time_rm}"
            echo "压缩目录rm $db_backup_fulldir${time_rm}" >>/home/backup/config/tar.log
        fi
    fi

    if [ $1 -gt 0 -a $1 -lt 7 ]; then
        if [ -d $db_backup_incrementaldir${time_rm} ]; then
            su - root -c "tar -czPvf ${db_backup_tarfull}${time_rm}_increment.tar.gz ${db_backup_incrementaldir}${time_rm}"
            su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm}"
            echo "压缩目录rm $db_backup_incrementaldir${time_rm}" >>/home/backup/config/tar.log
        fi
    fi
}

#得到当前时间和配置文件
time="$(date +"back_%d-%m-%Y")"
source /home/backup/config/config

#计算今天到上次全备份相隔天数
_Day=$(date2days $(echo ${backup_full:5:10} | awk 'BEGIN{FS="-"}{print $3,$2,$1}'))
Day=$(date2days $(date +"%Y %m %d"))
echo $_Day
echo $Day
let result=$Day-$_Day
echo "离最近一次全备份相差$result天"

if [ -z ${backup_full} ] || [ $result -ge 7 ]; then
    #todo
    echo '全备份'
    backup_full=${time}
    innobackupex --defaults-file=$db_defaults_file --no-timestamp --user=${db_user} --password=${db_passwd} --databases=${db_databases} --no-lock --socket=$db_socket ${db_backup_fulldir}${backup_full}/

    if [ $? -eq 0 ]; then
        echo "${time} 备份成功!!!" >>/home/backup/config/results.log
    else
        echo "${time} 备份失败???" >>/home/backup/config/results.log
    fi

    #更新配置文件
    echo "backup_full=${backup_full}" >/home/backup/config/config
    echo "backup_pre_name=full/${backup_full}" >>/home/backup/config/config

    #自动删除两周以前的备份文件
    while [ ${rm_num} -lt 8 -a ${rm_num} -gt 0 ]; do
        move_and_tar ${rm_num}
        rm_num=$(expr ${rm_num} - 1)
    done

    echo '全备份'
else
    #todo
    echo '增量备份'

    innobackupex --defaults-file=$db_defaults_file --socket=$db_socket --no-timestamp --user=${db_user} --password=${db_passwd}  --databases=${db_databases} --no-lock --incremental ${db_backup_incrementaldir}${time}/ --incremental-basedir=${db_backup}${backup_pre_name}

    if [ $? -eq 0 ]; then
        echo "${time} 增量备份成功!!!" >>/home/backup/config/results.log
    else
        echo "${time} 增量备份失败???" >>/home/backup/config/results.log
    fi

    #更新配置文件
    echo "backup_full=${backup_full}" >/home/backup/config/config
    echo "backup_pre_name=incremental/${time}" >>/home/backup/config/config
    echo '增量备份'
fi
